from contents.models import ContentCategory
from goods.models import GoodsChannel


def get_categories():
    # 获取频道
    channels = GoodsChannel.objects.all()
    categories = {}
    for channel in channels:
        if channel.group_id not in categories:
            categories[channel.group_id] = {'channels': [], 'sub_cate': []}

        # 获取一级目录
        cate1 = channel.category
        categories[channel.group_id]['channels'].append({
            'id': cate1.id,
            'name': cate1.name,
            'url': channel.url
        })

        # 获取二级目录
        cate2 = channel.category.subs.all()

        categories[channel.group_id]['sub_cate'].extend(cate2)

    return categories

def get_contents():
    contents = {}
    content_categorys = ContentCategory.objects.all()
    for content_category in content_categorys:
        contents[content_category.key] = content_category.content_set.all()

    return contents

